package com.cskaoyan.wordmemorize.controller.app;

import com.cskaoyan.wordmemorize.dto.admin.GoodsDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.GoodsPageRequest;
import com.cskaoyan.wordmemorize.service.GoodsService;
import com.cskaoyan.wordmemorize.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * App端-商品相关接口
 */
@RestController
@RequestMapping("/product")
public class AppGoodsController {

    @Resource
    private GoodsService goodsService;

    /**
     * 分页获取指定类型商品（如单词上限、补签卡）
     *
     * @param pageNumber 页码（从前端传递）
     * @param count 每页数量（从前端传递）
     * @param types 商品类型列表
     * @param keyword 关键词
     * @return PageDTO<GoodsDTO> 商品 分页查询结果
     */
    @Operation(summary = "分页查询指定类型商品", description = "只查type为1(单词上限)、3(补签卡)的商品")
    @GetMapping
    public Result<PageDTO<GoodsDTO>> getGoodsPage(
            @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
            @RequestParam(value = "count", required = false) Integer count,
            @RequestParam(value = "types", required = false) List<Integer> types,
            @RequestParam(value = "keyword", required = false) String keyword) {

        // 创建分页请求对象并正确设置参数
        GoodsPageRequest goodsPageRequest = new GoodsPageRequest();
        
        // 处理页码，如果是0则设置为1（因为MyBatis-Plus从1开始计数）
        goodsPageRequest.setPageNum(pageNumber != null && pageNumber > 0 ? pageNumber : 1);
        
        // 处理每页数量，如果是0则设置为10
        goodsPageRequest.setCount(count != null && count > 0 ? count : 10);
        
        // 设置商品类型和关键词
        goodsPageRequest.setTypes(types);
        goodsPageRequest.setKeyword(keyword);

        // 调用Service层
        PageDTO<GoodsDTO> goodsDTO = goodsService.getGoodsPage(goodsPageRequest);

        // 返回goodsDTO 商品信息 分页查询结果
        return Result.ok(goodsDTO);
    }
}